package com.wbt.bambootown.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.DevicesData;
import com.wbt.bambootown.mvp.contract.ObsDevicesContract;
import com.wbt.bambootown.mvp.frame.MvpFragment;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.ObsDevicesPresenter;
import com.wbt.bambootown.ui.activity.IconDataActivity;
import com.wbt.bambootown.ui.adapter.DevicesDataAdapter;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.Log;
import com.wbt.bambootown.utils.SPUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wufan on 2018/5/15.
 */

public class ObsDevicesFragment extends MvpFragment<ObsDevicesPresenter> implements ObsDevicesContract.View, DevicesDataAdapter.onItemClick, OnTimeSelectListener, View.OnClickListener {

    @BindView(R.id.rv_data)
    RecyclerView mRvData;
    @BindView(R.id.rl_date)
    RelativeLayout mRlDate;
    @BindView(R.id.tv_beginTime)
    TextView mTvBeginTime;
    @BindView(R.id.tv_endTime)
    TextView mTvEndTime;
    @BindView(R.id.bt_sure)
    Button mBtSure;
    @BindView(R.id.bt_cancel)
    Button mBtCancel;

    private DevicesDataAdapter mAdapter;
    private TimePickerView timePicker;

    //判断是否焦点在开始时间上
    private boolean isBeginTime = true;
    private int currentId;
    private String currentName;

    private SimpleDateFormat sf;
    private Calendar c;

    @Override
    protected boolean userDefaultStatus() {
        return true;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mAdapter = new DevicesDataAdapter(getContext());
        mRvData.setLayoutManager(layoutManager);
        mRvData.setAdapter(mAdapter);
        mAdapter.setListener(this);

        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.set(c.get(Calendar.YEAR) , c.get(Calendar.MONTH) , c.get(Calendar.DATE) , 0 , 0 , 0);
        String beginTime = sf.format(c.getTime());
        String endTime = sf.format(Calendar.getInstance().getTime());

        mTvBeginTime.setText(SPUtils.getString(Cons.DateSerach.BEGINTIME) == null ? beginTime : SPUtils.getString(Cons.DateSerach.BEGINTIME));
        mTvEndTime.setText(SPUtils.getString(Cons.DateSerach.ENDTIME) == null ?  endTime: SPUtils.getString(Cons.DateSerach.ENDTIME));

        SPUtils.putString(Cons.DateSerach.BEGINTIME , beginTime);
        SPUtils.putString(Cons.DateSerach.ENDTIME , endTime);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {
        mPresenter.getCollectDevices(FrameApp.currentFarmId);
        showProgressDialog();
        initTimePicker();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {

    }

    @Override
    public int getContentView() {
        return R.layout.fragment_obsdevices;
    }

    @Override
    protected boolean isApplyEventBus() {
        return true;
    }

    @Override
    protected boolean isApplyButterKnife() {
        return true;
    }

    @Override
    protected void onEventCome(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == Cons.Event.EVENT_DEVICEE_DATA) {
            mPresenter.getCollectDevices(FrameApp.currentFarmId);
        }
    }

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(getContext(), errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDataSuccess(List<DevicesData> devicesDatas) {
        dismissProgressDialog();
        mAdapter.setList(devicesDatas);
    }

    @Override
    public void query(int id , String name) {
        currentId = id;
        currentName = name;
        mRlDate.setVisibility(View.VISIBLE);
    }

    private void initTimePicker() {
        timePicker = new TimePickerBuilder(getContext(), this)
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确认")//确认按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleText("查询时间")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(getContext().getResources().getColor(R.color.theme_color))//标题文字颜色
                .setSubmitColor(getContext().getResources().getColor(R.color.theme_color))//确定按钮文字颜色
                .setCancelColor(getContext().getResources().getColor(R.color.theme_color))//取消按钮文字颜色
                .setTitleBgColor(getContext().getResources().getColor(R.color.white))//标题背景颜色 Night mode
                .setBgColor(getContext().getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                .setRangDate(null, Calendar.getInstance())//起始终止年月日设定
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false)//是否显示为对话框样式
                .build();
    }

    @Override
    public void onTimeSelect(Date date, View v) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(isBeginTime){
            mTvBeginTime.setText(sdf.format(date));
            SPUtils.putString(Cons.DateSerach.BEGINTIME , sdf.format(date));
        }else{
            mTvEndTime.setText(sdf.format(date));
            SPUtils.putString(Cons.DateSerach.ENDTIME , sdf.format(date));
        }
    }

   @OnClick({R.id.tv_beginTime , R.id.tv_endTime , R.id.bt_sure , R.id.bt_cancel})
    public void onClick(View view){
       switch (view.getId()){
           case R.id.tv_beginTime:
               isBeginTime = true;
               timePicker.show();
               break;
           case R.id.tv_endTime:
               isBeginTime = false;
               timePicker.show();
               break;
           case R.id.bt_sure:
               mRlDate.setVisibility(View.GONE);
               Intent intent = new Intent(getContext() , IconDataActivity.class);
               intent.putExtra("id" , currentId);
               intent.putExtra("name" , currentName);
               startActivity(intent);
               break;
           case R.id.bt_cancel:
               mRlDate.setVisibility(View.GONE);
               break;
       }
   }
}
